//绘制散点图
function isInPolygon(checkPoint, polygonPoints) {
    var counter = 0;
    var i;
    var xinters;
    var p1, p2;
    var pointCount = polygonPoints.length;
    p1 = polygonPoints[0];

    for (i = 1; i <= pointCount; i++) {
        p2 = polygonPoints[i % pointCount];
        if (
            checkPoint[0] > Math.min(p1[0], p2[0]) &&
            checkPoint[0] <= Math.max(p1[0], p2[0])
        ) {
            if (checkPoint[1] <= Math.max(p1[1], p2[1])) {
                if (p1[0] != p2[0]) {
                    xinters =
                        (checkPoint[0] - p1[0]) *
                        (p2[1] - p1[1]) /
                        (p2[0] - p1[0]) +
                        p1[1];
                    if (p1[1] == p2[1] || checkPoint[1] <= xinters) {
                        counter++;
                    }
                }
            }
        }
        p1 = p2;
    }
    if (counter % 2 == 0) {
        return false;
    } else {
        return true;
    }
}
function drawScatterMap() {
    initdata();
    for(var i = 1;i<51;i++)
    {
        var rand
        if(i >= 3 && i <= 6 ||
           i >= 21 && i <= 26 ||
           i >= 34 && i <= 38)
        {
            rand = 50;
        }
        else
        {
            rand = 20;
        }
        var vertices = getScatterMapVertexs(i,rand);
        //getScatterMapColors(i,rand);
        createScatterMap(vertices,scatterMapcolors);
    }
    for(var i = 0;i<g_sandiangroup.children.length;i++)
    {
        g_sandiangroup.children[i].visible = false;
    }
}
function generateSprite(randomnum) {


    var canvas = document.createElement( 'canvas' );
    canvas.width = 32;
    canvas.height = 32;

    var context = canvas.getContext( '2d' );
    var gradient = context.createRadialGradient( canvas.width / 2, canvas.height / 2, 0, canvas.width / 2, canvas.height / 2, canvas.width / 2 );
    //var randomnum = Math.ceil(Math.random()*10)%3;
    //var randomnum = 0;

    if(randomnum == 2)
    {
        gradient.addColorStop( 0, 'rgba(218,220,222,0.6)' );
        gradient.addColorStop( 0.3, 'rgba(218,220,222,0.6)' );
        gradient.addColorStop( 0.8, 'rgba(218,0,0,0.6)' );
        gradient.addColorStop( 1, 'rgba(0,0,0,1)' );
    }
    else if(randomnum == 1)
    {

        gradient.addColorStop( 0, 'rgba(45,209,212,0.6)' );
        gradient.addColorStop( 0.3, 'rgba(45,209,212,0.6)' );
        gradient.addColorStop( 0.8, 'rgba(45,209,212,0.6)' );
        gradient.addColorStop( 1, 'rgba(0,0,0,1)' );
    }
    else if(randomnum == 0)
    {
        gradient.addColorStop( 0, 'rgba(46,127,218,0.6)' );
        gradient.addColorStop( 0.3, 'rgba(46,127,218,0.6)' );
        gradient.addColorStop( 0.8, 'rgba(46,127,218,0.6)' );
        gradient.addColorStop( 1, 'rgba(0,0,0,1)' );
    }



    context.fillStyle = gradient;
    context.fillRect( 0, 0, canvas.width, canvas.height );

    return canvas;
}
function createScatterMap(tempVertices, tempColors)
{
    var sandiangroup = new THREE.Group();
    for ( var i = 0; i < tempVertices.length;i+=3 ) {
        var randomnum = Math.ceil(Math.random()*10)%3;
        var sprite = new THREE.CanvasTexture( generateSprite(0));

        var material = new THREE.SpriteMaterial( {
            map: sprite,
            blending: THREE.AdditiveBlending,
            depthTest:false
        } );

        particle = new THREE.Sprite( material );
        //particle.scale.x = particle.scale.y = randomnum+1;
        particle.scale.x = particle.scale.y = 2;
        particle.position.set(tempVertices[i+0],tempVertices[i+1],tempVertices[i+2]);
        sandiangroup.add( particle );
    }
    g_sandiangroup.add( sandiangroup );
    g_sandiangroup.rotation.x = -0.5*Math.PI;
    g_sandiangroup.rotation.z = -0.5*Math.PI;
    g_scene.add( g_sandiangroup );
    //g_sandiangroup.visible = false;
}
function getScatterMapVertexs(count,rand) {
    var vertices = [];
    var tempZ;
    for(var i = 0;i<scatterZ.length;i++)
    {
        if(getFloorNumFromNameId(scatterZ[i][0]) == count)
        {
            
            tempZ = scatterZ[i][1];
            break;
        }
    }
    
    for ( var i = 0; i < rand; i ++ )
    {
        
        var tempX = Math.random()*150;
        var tempY = Math.random()*150;

        if(count >= 3 && count <= 6)
        {
            if(isInPolygon([tempX,tempY],scatterploygn1))
            {
                vertices.push( tempX, tempY, tempZ );
            }
        }
        else if(count >= 21 && count <= 26)
        {
            if(isInPolygon([tempX,tempY],scatterploygn3))
            {
                vertices.push( tempX, tempY, tempZ );
            }
        }
        else if(count >= 34 && count <= 38)
        {
            if(isInPolygon([tempX,tempY],scatterploygn4))
            {
                vertices.push( tempX, tempY, tempZ );
            }
        }
        else if(count >= 42)
        {
            if(isInPolygon([tempX,tempY],scatterploygn21))
            {
                vertices.push( tempX, tempY, tempZ );
            }
        }
        else
        {
            if(isInPolygon([tempX,tempY],scatterploygn21) || isInPolygon([tempX,tempY],scatterploygn22))
            {
                vertices.push( tempX, tempY, tempZ );
            }
        }
    }
    return vertices;
    
}
function getScatterMapColors(count,rand) {

    for ( var i = 0; i < rand; i ++ ) {

        var colornum = Math.ceil(Math.random()*10)%3;
        if(colornum == 0)
        {
            scatterMapcolors.push(1,0,0);
        }
        else if(colornum == 1)
        {
            scatterMapcolors.push(0,1,0);
        }
        else
        {
            scatterMapcolors.push(0,0,1);
        }
    }
}